<?php

class User
{
    /** @var int */
    protected $id = 0;
    /** @var string */
    protected $email = '';
    /** @var string */
    protected $nickname = '';
    /** @var string */
    protected $password = '';
    /** @var string */
    protected $salt = '';
    /** @var int */
    protected $power = 0;
    /** @var string */
    protected $regIp = '';
    /** @var string */
    protected $regTime = '2000-01-01 00:00:00';
    /** @var string */
    protected $loginIp = null;
    /** @var string */
    protected $loginTime = null;
    /** @var string */
    protected $actIp = null;
    /** @var string */
    protected $actTime = null;
    /** @var int */
    protected $userInfoId = null;
    /** @var int */
    protected $studentInfoId = null;

    /**
     * @return string
     */
    public function getDisplayName()
    {
        if (!$this->getId())
            return '游客';
//        if ($this->getStudentInfoId() > 0)
//            return $this->getStudentInfo()->getStudentName();
        if ($this->getNickname())
            return $this->getNickname();
        if ($this->getEmail())
            return $this->getEmail();
        return '[' . $this->getId() . '|DisplayName]';
    }

    /**
     * @return string
     */
    public function getDisplayContact()
    {
        if (!$this->getUserInfoId())
            return $this->getEmail();

        $thatUserInfo = $this->getUserInfo();

        if ($thatUserInfo->getPhone())
            return '电话: ' . $thatUserInfo->getPhone();

        if ($thatUserInfo->getQq())
            return 'QQ: ' . $thatUserInfo->getQq();

        return '[未知]';
    }

    /**
     * @return string
     */
    public function getDisplayAvatarUrl()
    {
        $thatFile = File_Model::getFileByHash($this->getUserInfo()->getAvatarHash());
        if ($thatFile)
            return base_url($thatFile->getUrl());

        if(stristr($this->getUserInfo()->getAvatarHash(),'#OLD_PATH#')){
            preg_match('/#OLD_PATH#__ROOT__(.*?).jpg/i',$this->getUserInfo()->getAvatarHash(),$result);
            return $result[1].'.jpg';
        }
        return base_url('public/images/default.gif');
    }

    public function getDisplayCountCommodity()
    {
        return sizeof(Commodity_Model::getCommodityArrayByUserId($this->getId()));
    }

    public function getDisplayCountCommodityRequire()
    {
        return sizeof(CommodityRequire_Model::getCommodityRequireArrayByUserId($this->getId()));
    }

    public function getDisplayCountCommodityFavorite()
    {
        return CommodityFavorite_Model::countCommodityFavoriteByUserId($this->getId());
    }

    /**
     * @return UserInfo
     */
    public function getUserInfo()
    {
        if ($this->getUserInfoId()) {
            return UserInfo_Model::getUserInfoById($this->getUserInfoId());
        }

        $userInfo = new UserInfo();
        $userInfo->setUserId($this->getId());
        return $userInfo;
    }

    /**
     * @return StudentInfo
     */
    public function getStudentInfo()
    {
        if ($this->getStudentInfoId()) {
            return StudentInfo_Model::getStudentInfoById($this->getStudentInfoId());
        }
        $studentInfo = new StudentInfo();
        $studentInfo->setUserId($this->id);
        return $studentInfo;
    }

    /**
     * @return array
     */
    public function getDataArray()
    {
        return array(
            'id'            => $this->getId(),
            'email'         => $this->getEmail(),
            'nickname'      => $this->getNickname(),
            'power'         => $this->getPower(),
            'regIp'         => $this->getRegIp(),
            'regTime'       => $this->getRegTime(),
            'loginIp'       => $this->getLoginIp(),
            'loginTime'     => $this->getLoginTime(),
            'actIp'         => $this->getActIp(),
            'actTime'       => $this->getActTime(),
            'userInfoId'    => $this->getUserinfoId(),
            'studentInfoId' => $this->getStudentinfoId(),
            'schoolunicode' => $this->getStudentInfo()->getSchool()->getUnicode(),
        );
    }

    /**
     * @return bool
     */
    public function isAdministrator()
    {
        if ($this->getPower() >= 99999) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param string $actIp
     */
    public function setActIp($actIp)
    {
        $this->actIp = $actIp;
    }

    /**
     * @return string
     */
    public function getActIp()
    {
        return $this->actIp;
    }

    /**
     * @param string $actTime
     */
    public function setActTime($actTime)
    {
        $this->actTime = $actTime;
    }

    /**
     * @return string
     */
    public function getActTime()
    {
        return $this->actTime;
    }

    /**
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $loginIp
     */
    public function setLoginIp($loginIp)
    {
        $this->loginIp = $loginIp;
    }

    /**
     * @return string
     */
    public function getLoginIp()
    {
        return $this->loginIp;
    }

    /**
     * @param string $loginTime
     */
    public function setLoginTime($loginTime)
    {
        $this->loginTime = $loginTime;
    }

    /**
     * @return string
     */
    public function getLoginTime()
    {
        return $this->loginTime;
    }

    /**
     * @param string $nickname
     */
    public function setNickname($nickname)
    {
        $this->nickname = $nickname;
    }

    /**
     * @return string
     */
    public function getNickname()
    {
        return $this->nickname;
    }

    /**
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * @param int $power
     */
    public function setPower($power)
    {
        $this->power = $power;
    }

    /**
     * @return int
     */
    public function getPower()
    {
        return $this->power;
    }

    /**
     * @param string $regIp
     */
    public function setRegIp($regIp)
    {
        $this->regIp = $regIp;
    }

    /**
     * @return string
     */
    public function getRegIp()
    {
        return $this->regIp;
    }

    /**
     * @param string $regTime
     */
    public function setRegTime($regTime)
    {
        $this->regTime = $regTime;
    }

    /**
     * @return string
     */
    public function getRegTime()
    {
        return $this->regTime;
    }

    /**
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * @return string
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * @param int $studentInfoId
     */
    public function setStudentInfoId($studentInfoId)
    {
        $this->studentInfoId = $studentInfoId;
    }

    /**
     * @return int
     */
    public function getStudentInfoId()
    {
        return $this->studentInfoId;
    }

    /**
     * @param int $userInfoId
     */
    public function setUserInfoId($userInfoId)
    {
        $this->userInfoId = $userInfoId;
    }

    /**
     * @return int
     */
    public function getUserInfoId()
    {
        return $this->userInfoId;
    }


}